import axios from 'axios'
import Element from 'element-ui'
import router from './router'
import store from './store'

axios.defaults.baseURL = "http://localhost:8881"

//配置拦截器
axios.interceptors.request.use(config => {
    return config
})

axios.interceptors.response.use(response => {
    let data = response.data;

    if (data.code === 200) {
        return response
    } else {
        //打印错误消息，并且停留3s
        Element.Message.error(data.msg, {duration: 3 * 1000});
        return Promise.reject(data.msg)
    }
}, error => {
    if (error.response.data) {
        error.message = error.response.data.msg
    }

    //如果没有权限的话，就跳转到登录页面
    if (error.response.status === 401){
        store.commit('REMOVE_INFO')
        router.push('/login')
    }
    Element.Message.error(error.message, {duration: 3 * 1000});
    return Promise.reject(error.message)
})